<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html style="direction: ltr;" lang="en">
<head>





  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">





  <meta name="Generator" content="Internet Assistant for Word Version 3.0">




  <title>example</title>
</head>


<body>



<h1 style="text-align: center;">COID  - An example program</h1>


<br>


<br>
<a href="http://sourceforge.net/project/showfiles.php?group_id=82042&amp;package_id=163787">download source code</a><br>


<h3>The service header file</h3>




<dir>
<dir>
</dir>



</dir>




That's the core of our example program. This header file contains the exported class. <br>




<br>



<code>&nbsp;&nbsp;&nbsp; #include "coid/coidsvc/coidsvc.h"<br>


<br>


</code>The header file always has to include coidsvc.h.<code><br>


<br>


&nbsp;&nbsp;&nbsp; #include "string.h"<br>


&nbsp;&nbsp;&nbsp; class TestClass<br>


&nbsp;&nbsp;&nbsp; {<br>


&nbsp;&nbsp;&nbsp; int status;<br>


<br>


&nbsp;&nbsp;&nbsp; public:<br>


&nbsp;&nbsp;&nbsp; TestClass()<br>


&nbsp;&nbsp;&nbsp; {       <br>


&nbsp;&nbsp;&nbsp; }<br>


</code>






<br>



The exported class may contain a constructor, but it must not have
arguments. If you want to use arguments when you create your class, use
<code>accept_connect()</code> for it.<br>



There is also the <code>accept_connect_shared()</code> and the  <code>accept_connect_auto()</code> method. For more information about this read <a href="../coid.html#methods">Special methods</a><br>


<br>



<code>&nbsp;&nbsp;&nbsp; net_fnc_s opcd accept_connect(int status_init)</code><br>



<br>


The <code>accept_connect()</code> method is called as soon as the client creates the exported class. It has to have the keyword <code>net_fnc_s</code> and <code>opcd</code> in front of it. <code>net_fnc_s</code> means that this is a special, exported class. <code>opcd</code> is a custom data type to store error information. The value for status_init can be initialized from the remote client.<br>



<code>        <br>


&nbsp;&nbsp;&nbsp; status = status_init;<br>


&nbsp;&nbsp;&nbsp; return 0;</code>
                <br>


<br>



As usual 0 means no error. If you want to know more about error codes and the opcd type, go to&nbsp;<a href="http://coid.sourceforge.net/opcd_type.html">http://coid.sourceforge.net/opcd_type.html</a><br>



It's not mandatory to use opcd, but recommended to cause no problems with error handling in case of simultaneous calls.<br>



        <br>



        <code>// returns 'length' of 'str'<br>


        net_fnc opcd get_length( net_out int &amp; length, const char * str ) <br>


</code>
<br>



This is now a standard exported method. The net_fnc keyword tells
coidgen that this method is meant to be exported. opcd is as before the
error return value. <br>


The arguments can have different decorations. net_out means that this
argument is a return value (server --&gt; client). Arguments without
decoration are automatically net_in, that's a equivalent to a standard
call by value (client --&gt; server). <br>


<br>


<code>length = strlen(str);<br>


return 0;<br>


</code>

        <br>




        <code>// multiplies 'a' and 'b' and returns 'result'<br>


        net_fnc opcd multiply( double a, double b, net_out double &amp; result ) <br>



        {       <br>


&nbsp;&nbsp;&nbsp; result = a * b;<br>


&nbsp;&nbsp;&nbsp; return 0;<br>



        }<br>


// multiplies 'a' and 'b' and returns 'result'<br>


        net_fnc opcd add_one( net_inout int &amp; number ) <br>


</code>





<br>


This method has a new decoration: net_inout. That's the equivalent to a call by reference (client &lt;--&gt; server).<br>


<code>
                &nbsp;&nbsp;&nbsp; number++;<br>


&nbsp;&nbsp;&nbsp; return 0;<br>


</code>
                <br>




        <code>// sets 'status' variable<br>


        net_fnc opcd set_status( net_out int &amp; old_status, int new_status ) <br>



        {<br>


&nbsp;&nbsp;&nbsp; old_status = status;<br>


&nbsp;&nbsp;&nbsp; status = new_status;<br>


&nbsp;&nbsp;&nbsp; return 0;<br>



        }<br>


</code>



<br>




So much about the service header file. Basically it's just a normal
class with a few methods, that are decorated with the keywords net_fnc,
net_out and net_inout. <br>








<br>


<h3>The client</h3>



<dir>
<dir>
<dir>
<dir>
</dir>



</dir>



</dir>



</dir>




The second file that is written manually and not generated is the main
client file. This client does actually not much more than to create the
remote class and call some methods. <br>




<br>



<code>#include "example_client.h"<br>


#include &lt;iostream&gt;<br>


</code>

<br>


The client has to include the generated _client.h file.<br>




<code><br>


int main() <br>



{<br>


&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "Connecting to coid server ... ";<br>


&nbsp;&nbsp;&nbsp; TestClass_client testcl;<br>


&nbsp;&nbsp;&nbsp; opcd e = testcl.connect( 3, "localhost:55099" );<br>


</code>


        <br>



This creates the exported class by connecting to coid server (at
localhost:55099) and submit '3' as constructor (accept_connect)
parameter for the status variable. <br>




        <br>



The next lines are doing the error handling. If 'e' is true, an error
has occurred. In this case the command opcd_formatter(e) returns a
string explaining the error.<br>



        <code>&nbsp;&nbsp;&nbsp; if(e) <br>


&nbsp;&nbsp;&nbsp; {      <br>


&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "failed with error \"" &lt;&lt; opcd_formatter(e) &lt;&lt; "\".\n";<br>


&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return false;<br>


&nbsp;&nbsp;&nbsp; } <br>


&nbsp;&nbsp;&nbsp; else<br>


&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; std::cout &lt;&lt;"successful. \n";<br>


</code>




        <br>



        That's an example for a call of the method get_length of the
exported class.<br>



        <br>


<code>&nbsp;&nbsp;&nbsp; int str_length;<br>


&nbsp;&nbsp;&nbsp; char teststring[5] = "test";<br>


&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "Call get_length ... ";<br>


&nbsp;&nbsp;&nbsp; e = testcl.get_length( str_length, "test" );<br>


</code>



<br>


Error handling works in the same way as above.<br>


<br>



        <code>&nbsp;&nbsp;&nbsp; if(e) { <br>


&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "failed with error \"" &lt;&lt; opcd_formatter(e) &lt;&lt; "\".\n";<br>


&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return false;<br>


&nbsp;&nbsp;&nbsp; }<br>


&nbsp;&nbsp;&nbsp;&nbsp;else<br>


&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; std::cout &lt;&lt; "The string
\"" &lt;&lt; teststring &lt;&lt; "\" contains " &lt;&lt; str_length
&lt;&lt; " characters.\n";<br>


</code>




        <br>




There are more example method calls in the original client.cpp.        <br>




        <br>
<a href="http://sourceforge.net/project/showfiles.php?group_id=82042&amp;package_id=163787">download source code</a><br>
<br>


</body>
</html>
